package org.hackystat.sdsa.analyzer.control.processor;

import java.util.Map;
import java.util.logging.Logger;

import org.hackystat.sdsa.analyzer.control.SensorDataUtil;
import org.hackystat.sdsa.analyzer.model.activity.Activity;
import org.hackystat.sdsa.analyzer.model.activity.refactoring.BinaryRefactorActivity;
import org.hackystat.sdsa.analyzer.model.activity.refactoring.RefactorActivity;
import org.hackystat.sdsa.analyzer.model.activity.refactoring.RefactorOperator;
import org.hackystat.sdsa.analyzer.model.activity.refactoring.UnaryRefactorActivity;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;

/**
 * Implements the refactoring activity processor.
 * 
 * @author hongbing
 *
 */
public class RefactoringActivityProcessor implements Processor {

  /**
   * Process the sensor data to generate activities.
   * 
   * @param sensorData Sensor data.
   * @param logger Logger.
   * @return Base activity.
   */
  public Activity process(SensorData sensorData, Logger logger) {
    Map<String, String> propMap = SensorDataUtil.propMap(sensorData);
    String op = propMap.get(RefactorActivity.SUBSUB_TYPE);
    // Refactor operator (Add, Remove, Rename or move)
    if ("Refactor".equals(op)) {
      op = "Rename";
    }
    
    RefactorOperator operator = RefactorOperator.getInstance(op);
    if (operator.isBinary()) {
      return new BinaryRefactorActivity(sensorData, logger);
    }
    else {
        return new UnaryRefactorActivity(sensorData, logger);
    }
  }

  /**
   * Tests whether this provided sensor data is processable.
   * 
   * @param sensorData sensorData.
   * @return True if it is a refactoring activity.
   */
  public boolean processable(SensorData sensorData) {
    String sensorDataType = sensorData.getSensorDataType();
    Map<String, String> propMap = SensorDataUtil.propMap(sensorData);
    
    if (!"DevEvent".equalsIgnoreCase(sensorDataType)) {
      return false;
    }
    
    if (!propMap.containsKey("type") || !"Edit".equalsIgnoreCase(propMap.get("type"))) {
      return false;
    }
    
    if (!propMap.containsKey("subtype") || 
             !"ProgramUnit".equalsIgnoreCase(propMap.get("subtype"))) {
       return false;     
    }
    
    return true;  
  }
}
